package com.ktwlrj.core.base;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktwlrj.core.util.KtwlStringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 基础入参参数(代分页)
 *
 * @author zhouxuanhong
 * @date 2019-05-10 15:12
 * @since JDK1.8
 */
@Data
@ApiModel(value = "分页请求数据request", description = "分页请求参数")
public class SuperPageArgs implements Serializable {

    private static final long serialVersionUID = 1L;
    @ApiModelProperty(name = "currentPage", value = "当前页")
    private Long currentPage;

    @ApiModelProperty(name = "size", value = "每页条数")
    private Long size;

    @ApiModelProperty(name = "descs", value = "降序字段列表")
    private List<String> descs;

    @ApiModelProperty(name = "ascs", value = "升序字段列表")
    private List<String> ascs;

    @ApiModelProperty(name = "regionCode", value = "用户当前所属地区编码", hidden = true)
    private String regionCode;

    @ApiModelProperty(name = "orgId", value = "用于当前所属机构编码", hidden = true)
    private String orgId;

    @ApiModelProperty(name = "memberId", value = "会员id", hidden = true)
    private String memberId;

    @ApiModelProperty(name = "queryPage", value = "myBatisPlus分页信息", hidden = true)
    public <T> Page<T> getQueryPage() {
        if (this.currentPage == null || this.currentPage < 0) {
            this.currentPage = 0L;
        }
        if (this.size == null || this.size <= 0) {
            this.size = 10L;
        }
        Page<T> page = new Page<>(this.currentPage, this.size);
        if (this.descs != null && !this.descs.isEmpty()) {
            this.descs.forEach(v -> {
                OrderItem orderItem = OrderItem.desc(KtwlStringUtils.humpToUnderline(v));
                page.addOrder(orderItem);
            });
        } else if (this.ascs != null && !this.ascs.isEmpty()) {
            this.ascs.forEach(v -> {
                OrderItem orderItem = OrderItem.asc(KtwlStringUtils.humpToUnderline(v));
                page.addOrder(orderItem);
            });
        }
        return page;
    }
}
